home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / gr564s.zip / SRC / RCSMERGE.C < prev    next >
C/C++ Source or Header  |  1992-09-05  |  7KB  |  260 lines

  1. /*
  2.  *                       rcsmerge operation
  3.  */
  4. /*****************************************************************************
  5.  *                       join 2 revisions with respect to a third
  6.  *****************************************************************************
  7.  */
  8.  
  9. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  10.    Copyright 1990, 1991, 1992 by Paul Eggert
  11.    Distributed under license by the Free Software Foundation, Inc.
  12.  
  13. This file is part of RCS.
  14.  
  15. RCS is free software; you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License as published by
  17. the Free Software Foundation; either version 2, or (at your option)
  18. any later version.
  19.  
  20. RCS is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. GNU General Public License for more details.
  24.  
  25. You should have received a copy of the GNU General Public License
  26. along with RCS; see the file COPYING.  If not, write to
  27. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  28.  
  29. Report problems and direct all questions to:
  30.  
  31.     rcs-bugs@cs.purdue.edu
  32.  
  33. */
  34.  
  35.  
  36.  
  37. /* $Log: rcsmerge.c,v $
  38.  * Revision 5.10  1992/07/28  16:12:44  eggert
  39.  * Add -V.
  40.  *
  41.  * Revision 5.9  1992/01/24  18:44:19  eggert
  42.  * lint -> RCS_lint
  43.  *
  44.  * Revision 5.8  1992/01/06  02:42:34  eggert
  45.  * Update usage string.
  46.  *
  47.  * Revision 5.7  1991/11/20  17:58:09  eggert
  48.  * Don't Iopen(f, "r+"); it's not portable.
  49.  *
  50.  * Revision 5.6  1991/08/19  03:13:55  eggert
  51.  * Add -r$.  Tune.
  52.  *
  53.  * Revision 5.5  1991/04/21  11:58:27  eggert
  54.  * Add -x, RCSINIT, MS-DOS support.
  55.  *
  56.  * Revision 5.4  1991/02/25  07:12:43  eggert
  57.  * Merging a revision to itself is no longer an error.
  58.  *
  59.  * Revision 5.3  1990/11/01  05:03:50  eggert
  60.  * Remove unneeded setid check.
  61.  *
  62.  * Revision 5.2  1990/09/04  08:02:28  eggert
  63.  * Check for I/O error when reading working file.
  64.  *
  65.  * Revision 5.1  1990/08/29  07:14:04  eggert
  66.  * Add -q.  Pass -L options to merge.
  67.  *
  68.  * Revision 5.0  1990/08/22  08:13:41  eggert
  69.  * Propagate merge's exit status.
  70.  * Remove compile-time limits; use malloc instead.
  71.  * Make lock and temp files faster and safer.  Ansify and Posixate.  Add -V.
  72.  * Don't use access().  Tune.
  73.  *
  74.  * Revision 4.5  89/05/01  15:13:16  narten
  75.  * changed copyright header to reflect current distribution rules
  76.  * 
  77.  * Revision 4.4  88/08/09  19:13:13  eggert
  78.  * Beware merging into a readonly file.
  79.  * Beware merging a revision to itself (no change).
  80.  * Use execv(), not system(); yield exit status like diff(1)'s.
  81.  * 
  82.  * Revision 4.3  87/10/18  10:38:02  narten
  83.  * Updating version numbers. Changes relative to version 1.1 
  84.  * actually relative to 4.1
  85.  * 
  86.  * Revision 1.3  87/09/24  14:00:31  narten
  87.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  88.  * warnings)
  89.  * 
  90.  * Revision 1.2  87/03/27  14:22:36  jenkins
  91.  * Port to suns
  92.  * 
  93.  * Revision 4.1  83/03/28  11:14:57  wft
  94.  * Added handling of default branch.
  95.  * 
  96.  * Revision 3.3  82/12/24  15:29:00  wft
  97.  * Added call to catchsig().
  98.  *
  99.  * Revision 3.2  82/12/10  21:32:02  wft
  100.  * Replaced getdelta() with gettree(); improved error messages.
  101.  *
  102.  * Revision 3.1  82/11/28  19:27:44  wft
  103.  * Initial revision.
  104.  *
  105.  */
  106. #include "rcsbase.h"
  107.  
  108. static char const co[] = CO;
  109.  
  110. mainProg(rcsmergeId, "rcsmerge", "$Id: rcsmerge.c,v 5.10 1992/07/28 16:12:44 eggert Exp $")
  111. {
  112.     static char const cmdusage[] =
  113.         "\nrcsmerge usage: rcsmerge -rrev1 [-rrev2] [-ksubst] [-{pq}[rev]] [-Vn] [-xsuff] file";
  114.     static char const quietarg[] = "-q";
  115.  
  116.     register int i;
  117.     char *a, **newargv;
  118.     char const *arg[3];
  119.     char const *rev[2]; /*revision numbers*/
  120.     char const *expandarg, *versionarg;
  121.         int tostdout;
  122.     int argc_min, status;
  123.     RILE *workptr;
  124.     struct buf commarg;
  125.     struct buf numericrev; /* holds expanded revision number */
  126.     struct hshentries *gendeltas; /* deltas to be generated */
  127.         struct hshentry * target;
  128.  
  129.     bufautobegin(&commarg);
  130.     bufautobegin(&numericrev);
  131.     argc_min = 1;
  132.     rev[0] = rev[1] = nil;
  133.     status = 0; /* Keep lint happy.  */
  134.     tostdout = false;
  135.     expandarg = versionarg = quietarg; /* i.e. a no-op */
  136.     suffixes = X_DEFAULT;
  137.  
  138.     argc = getRCSINIT(argc, argv, &newargv);
  139.     argv = newargv;
  140.     while (a = *++argv,  0<--argc && *a++=='-') {
  141.         switch (*a++) {
  142.                 case 'p':
  143.                         tostdout=true;
  144.             goto revno;
  145.  
  146.         case 'q':
  147.             quietflag = true;
  148.         revno:
  149.             if (!*a)
  150.                 break;
  151.                         /* falls into -r */
  152.                 case 'r':
  153.             if (!rev[0])
  154.                 rev[0] = a;
  155.             else if (!rev[1])
  156.                 rev[1] = a;
  157.             else
  158.                 faterror("too many revision numbers");
  159.                         break;
  160.         case 'x':
  161.             suffixes = a;
  162.             break;
  163.         case 'V':
  164.             versionarg = *argv;
  165.             argc_min = setRCSversion(versionarg);
  166.             break;
  167.  
  168.         case 'k':
  169.             expandarg = *argv;
  170.             if (0 <= str2expmode(expandarg+2))
  171.                 break;
  172.             /* fall into */
  173.                 default:
  174.             faterror("unknown option: %s%s", *argv, cmdusage);
  175.                 };
  176.         } /* end of option processing */
  177.  
  178.     if (argc < argc_min) faterror("no input file%s", cmdusage);
  179.     if (!rev[0]) faterror("no base revision number given");
  180.  
  181.     /* Now handle all pathnames.  */
  182.  
  183.     if (0  <  pairnames(argc, argv, rcsreadopen, true, false)) {
  184.  
  185.                 if (argc>2 || (argc==2&&argv[1]!=nil))
  186.                         warn("too many arguments");
  187.         diagnose("RCS file: %s\n", RCSname);
  188.         if (!(workptr = Iopen(workname, FOPEN_R_WORK, (struct stat*)0)))
  189.             efaterror(workname);
  190.  
  191.                 gettree();  /* reads in the delta tree */
  192.  
  193.                 if (Head==nil) faterror("no revisions present");
  194.  
  195.         if (!*rev[0])
  196.             rev[0]  =  Dbranch ? Dbranch : Head->num;
  197.         if (!fexpandsym(rev[0], &numericrev, workptr))
  198.             goto end;
  199.         if (!(target=genrevs(numericrev.string, (char *)nil, (char *)nil, (char *)nil,&gendeltas))) goto end;
  200.         rev[0] = target->num;
  201.         if (!rev[1] || !*rev[1])
  202.             rev[1]  =  Dbranch ? Dbranch : Head->num;
  203.         if (!fexpandsym(rev[1], &numericrev, workptr))
  204.             goto end;
  205.         if (!(target=genrevs(numericrev.string, (char *)nil, (char *)nil, (char *)nil,&gendeltas))) goto end;
  206.         rev[1] = target->num;
  207.  
  208.         if (strcmp(rev[0],rev[1]) == 0) {
  209.             if (tostdout) {
  210.                 FILE *o;
  211. #                if text_equals_binary_stdio || text_work_stdio
  212.                     o = stdout;
  213. #                else
  214.                     if (!(o=fdopen(STDOUT_FILENO,FOPEN_W_WORK)))
  215.                     efaterror("stdout");
  216. #                endif
  217.                 fastcopy(workptr,o);
  218.                 Ofclose(o);
  219.             }
  220.             goto end;
  221.         }
  222.         Izclose(&workptr);
  223.  
  224.         for (i=0; i<2; i++) {
  225.             diagnose("retrieving revision %s\n", rev[i]);
  226.             bufscpy(&commarg, "-p");
  227.             bufscat(&commarg, rev[i]);
  228.             if (run(
  229.                 (char*)0,
  230.                 /* Do not collide with merger.c maketemp().  */
  231.                 arg[i+1] = maketemp(i+3),
  232.                 co, quietarg, commarg.string, expandarg,
  233.                 versionarg, RCSname, (char*)0
  234.             ))
  235.                 faterror("co failed");
  236.         }
  237.         diagnose("Merging differences between %s and %s into %s%s\n",
  238.              rev[0], rev[1], workname,
  239.                          tostdout?"; result to stdout":"");
  240.  
  241.         arg[0] = rev[0] = workname;
  242.         status = merge(tostdout, rev, arg);
  243.         }
  244.  
  245. end:
  246.     Izclose(&workptr);
  247.     tempunlink();
  248.     exitmain(nerror ? DIFF_TROUBLE : status);
  249. }
  250.  
  251. #if RCS_lint
  252. #    define exiterr rmergeExit
  253. #endif
  254.     exiting void
  255. exiterr()
  256. {
  257.     tempunlink();
  258.     _exit(DIFF_TROUBLE);
  259. }
  260.